/*
 * @Author: sympsel sympsel@outlook.com
 * @Date: 2025-07-24 15:25:34
 * @LastEditors: sympsel sympsel@outlook.com
 * @LastEditTime: 2025-07-26 09:52:25
 * @FilePath: \work25_7_24\src\test.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Sort.h"

// 从AI那里粘贴的高精度计时器
double get_current_time() {
#ifdef _WIN32
    LARGE_INTEGER freq, counter;
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&counter);
    return (double)counter.QuadPart / freq.QuadPart;
#else
// 非windows系统
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return ts.tv_sec + ts.tv_nsec / 1e9;
#endif
}

int 可行性测试用例数 = 20;
int 随机数压力测试用例数 = 10000;

int 测试用例数据范围 = 100;   // 当 "随机数压力测试用例数"与"测试用例数据范围" 的比值越大，数据重复率越高

void PrintArray(int* arr, int n) {
    ARRAY_PRINT(arr, n, int, "%d");
}

void SortTest(const char* description, void (*sort)(int*, int)) {
    printf("[%s]\n", description);

    int n = 随机数压力测试用例数;

    int* arr = MALLOC(int, n);
    for (int i = 0; i < n; i++) {
        arr[i] = rand() % 测试用例数据范围;
    }
    if (n <= 20) {
        printf("Before Sort: ");
        PrintArray(arr, n);
    }
    
    double start = get_current_time();
    sort(arr, n);
    double end = get_current_time();
    double time_taken = end - start;
    if (n <= 20) {
        printf("After Sort:  ");
        PrintArray(arr, n);
    } else {
        double time_taken = end - start;
        printf("Run Time: %.7lf\n", time_taken);
    }

    free(arr);
    printf("[%s END]\n", description);
}

void QuickSortTest(const char* description, int (*partition)(int*, int, int), bool is_recursive) {
    printf("[%s]\n", description);

    int n = 随机数压力测试用例数;

    int* arr = MALLOC(int, n);
    for (int i = 0; i < n; i++) {
        arr[i] = rand() % 测试用例数据范围;
    }
    if (n <= 20) {
        printf("Before Sort: ");
        PrintArray(arr, n);
    }
    
    double start = get_current_time();
    QuickSortFrame(arr, n, partition, is_recursive);
    double end = get_current_time();
    double time_taken = end - start;
    if (n <= 20) {
        printf("After Sort:  ");
        PrintArray(arr, n);
    } else {
        double time_taken = end - start;
        printf("Run Time: %.7lf\n", time_taken);
    }

    free(arr);
    printf("[%s END]\n", description);
}

void test1() {
    SortTest("Select Sort", SelectSort);
    SortTest("Heap Sort", HeapSort);
    SortTest("Insert Sort", InsertSort);
    SortTest("Shell Sort", ShellSort);
    SortTest("Bubble Sort", BubbleSort);
    SortTest("Merge Sort Recursive", MergeSort);
    SortTest("Merge Sort", MergeSortNor);
}

// 快排专区
void test2() {
    QuickSortTest("Quick Sort Hoare Recursive", Partition1, true);
    QuickSortTest("Quick Sort dig_pit Recursive", Partition2, true);
    QuickSortTest("Quick Sort prev_cur_pointer Recursive", Partition3, true);
    QuickSortTest("Quick Sort three_apart Recursive", Partition4, true);

    QuickSortTest("Quick Sort Hoare", Partition1, false);
    QuickSortTest("Quick Sort dig_pit", Partition2, false);
    QuickSortTest("Quick Sort prev_cur_pointer", Partition3, false);
    QuickSortTest("Quick Sort three_apart", Partition4, false);
}

// 自省排序
void test3() {
    SortTest("Intro Sort", IntroSort);
}

int main() {
    srand((unsigned int)time(NULL));
    test1();
    test2();
    test3();
    return 0;
}